<?php

namespace app\model;

use think\Exception;
use think\Model;

class Applet extends Model
{
    // 设置当前模型对应的数据表
    protected $name = 'applet';
    // 设置当前模型主键
    protected $pk = 'id';

    protected $json = ['payment_rate','wechat_subscribe_template'];

    const DY_APP_KEY_1 = 'tt716807d5ee55bba601';    //比翼鸟剧场
    const DY_APP_KEY_2 = 'tt8772f1cf184f767a01';    //南源剧场
    const DY_APP_KEY_3 = 'ttb11cc7a12e214b3001';    //宁誉剧场
    const DY_APP_KEY_4 = 'tt8b39fd84140ac0bc01';    //比翼鸟短剧
    const DY_APP_KEY_5 = 'tt6bcb1d76f4f823b001';    //金豆豆剧场
    const DY_APP_KEY_6 = 'ttbb30cd8d2403f34801';    //金灿灿剧场
    const DY_APP_KEY_7 = 'ttab3fdd82340ce0b801';    //红彤彤剧场
    const DY_APP_KEY_8 = 'tt67c6117bf8fb962801';    //南誉剧场
    const DY_APP_KEY_9 = 'tt2b245d99b41760ab01';    //南西剧场
    const DY_APP_KEY_10 = 'tt0b2e7d93941d40a301';   //南誉短剧

    //公司主体
    const COMPANY_ENTITY_LIUYI = 1;//比翼鸟公司主体
    const COMPANY_ENTITY_DALIANQUMAN = 2;//大连趣漫主体
    const COMPANY_ENTITY_THREE = 3;//南宁源誉网络科技主体
    const AD_SCENE_1 = 'video';
    const AD_SCENE_2 = 'sign';
    const AD_SCENE_3 = 'activity';

    const AD_SCENE_LIST = [
        self::AD_SCENE_1 => '视频播放广告',
        self::AD_SCENE_2 => '用户签到广告',
        self::AD_SCENE_3 => '用户活动广告',
    ];

    // 添加/修改数据
    public function edit($params)
    {
        if (empty($params['id'])) {
            $data = $this::create($params);
            return $data->id;
        }
        $this::update($params);
        return $params['id'];
    }

    // 根据id获取数据
    public function getInfoById($id, $fields = 'id, name')
    {
        $data = $this::where(['id' => $id])->field($fields)->find();
        return $data ? $data->toArray() : [];
    }

    // 获取分页数据
    public function getPagingDataByWhere($where = [], $page = 1, $limit = 10, $fields = 'id, name', $order = 'id desc')
    {
        $total = $this::where($where)->count();

        if ($total == 0) {
            return ['total' => 0, 'total_page' => 0, 'data' => []];
        }

        $totalPage = ceil($total / $limit);
        $offset = ($page - 1) * $limit;

        $data = $this::where($where)->field($fields)->order($order)->limit($offset, $limit)->select()->toArray();

        return ['total' => $total, 'total_page' => $totalPage, 'data' => $data];
    }

    // 获取所有数据
    public function getDataByWhere($where = [], $fields = 'id, name', $order = 'id asc')
    {
        $data = $this::where($where)->field($fields)->order($order)->select();
        return $data ? $data->toArray() : [];
    }

    // 根据条件获取一条数据
    public function getInfoByWhere(array $where, $fields = 'id, name')
    {
        $data = $this::where($where)->field($fields)->find();
        return $data ? $data->toArray() : [];
    }

    /**
     * 获取支付比率
     * @param $paymentRate
     * @return array
     */
    public static function getPaymentRate($paymentRate): array
    {
        $configRate = config('enum.wechat_pay_random_probability');
        try {
            $res = [];
            $arr = [
                'zhangzhongfu' => PayRecord::PAY_MERCHANT_ZHANGZHONGYUN,
                'mihua_jump_liuyi' => PayRecord::PAY_MERCHANT_MIHUAAPPLETJUMP_LIUYI,
                'mihua_jump_dalianquman' => PayRecord::PAY_MERCHANT_MIHUAAPPLETJUMP_DALIANQUMAN,
                'virtual_payment' => PayRecord::PAY_MERCHANT_VIRTUALPAYMENT,
                'saomafu_payment' => PayRecord::PAY_MERCHANT_SAOMAFU,
            ];
            foreach ($paymentRate as $k => $item){
                $res[$arr[$k]] = $item;
            }
            if (array_sum($res) != 1){
                return $configRate;
            }
            return $res;
        }catch (\Exception $e){
            return $configRate;
        }
    }

    //返回广告id
    public static function getAdIdByScene($applet, $type): string
    {
        if (empty($applet)) return '';

        if ($applet['ad_id']) {
            $ad_id_list = json_decode($applet['ad_id'],true);
            $ad_id_list = array_column($ad_id_list,'value','key');
        } else {
            $ad_id_list = [];
        }
        $videoStr = $ad_id_list[self::AD_SCENE_1] ?? '';
        switch ($type) {
            case self::AD_SCENE_2:
                $adStr = $ad_id_list[self::AD_SCENE_2] ?? '';
                if (empty($adStr)) $adStr = $videoStr;
                break;
            case self::AD_SCENE_3:
                $adStr = $ad_id_list[self::AD_SCENE_3] ?? '';
                if (empty($adStr)) $adStr = $videoStr;
                break;
            default:
                $adStr = $videoStr;
                break;
        }
        if (empty($adStr)) return '';

        $adStr = str_replace('，', ',', $adStr);
        $adList = explode(',', $adStr);
        $index = array_rand($adList);

        return $adList[$index];
    }
}